﻿(function ($) {

    //Notification handler
    abp.event.on('abp.notifications.received', function (userNotification) {
        abp.notifications.showUiNotifyForUserNotification(userNotification);

        //Desktop notification
        Push.create("FwProject", {
            body: userNotification.notification.data.message,
            icon: abp.appPath + 'images/app-logo-small.png',
            timeout: 6000,
            onClick: function () {
                window.focus();
                this.close();
            }
        });
    });

    //serializeFormToObject plugin for jQuery
    $.fn.serializeFormToObject = function () {
        //serialize to array
        var data = $(this).serializeArray();

        //add also disabled items
        $(':disabled[name]', this).each(function () {
            data.push({ name: this.name, value: $(this).val() });
        });

        //map to object
        var obj = {};
        data.map(function (x) { obj[x.name] = x.value; });

        return obj;
    };

    //Configure blockUI
    if ($.blockUI) {
        $.blockUI.defaults.baseZ = 2000;
    }

})(jQuery);


function AjaxBegin() {
    layer.msg('数据已提交正在保存！', {
        icon: 6
    });
    $("#modal_ajax").modal("hide");
}

function LoadDataMessage() {
    layer.msg("数据加载中", {
        icon: 1
    });
}
function LoadDataSuccessMessage() {
    layer.closeAll();
}
function BackAjaxCallback(data) {
    CommonAjaxCallback(data, function () {
        self.location = document.referrer;
    });
}

function FailureAjaxCallback(xhr, status, thrown) {
    layer.closeAll('loading');
    var data = xhr.responseJSON;
    CommonAjaxCallback(data, function () {
        return false;
    });
}
//function CompleteAjaxCallback(xhr, status) {
//    var test = xhr.responseJSON;
//    console.log(xhr.responseText);
//    return;
//}
function CommonAjaxCallback(data, callback, successInfo) {

    if (data.success) {
        if (successInfo == null)
            callback();
        else {
            if (successInfo == undefined || successInfo == "success") successInfo = '操作成功';
            layer.msg(successInfo, {
                icon: 1,
                time: 2000
            }, function () {
                callback();
            });
        }
    }
    else if (data.unAuthorizedRequest) {
        layer.msg('登录信息丢失或你没有权限进行此操作，重新登录或切换登录帐号重试！', {
            icon: data.error.code,
            time: 2000
        });
    }
    else {

        layer.msg('操作失败：' + data.error.message, {
            icon: data.error.code,
        });
    }
}



function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

function dealDecimal(number) {
    var f_x = Math.round(number * 100) / 100; //强制保留2位小数
    var s_x = f_x.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}

function ReloadAjaxCallback(data, successInfo) {
    CommonAjaxCallback(data, function () {
        $("#modal_ajax").modal("hide");
        location.reload();
    }, successInfo);
}

function AjaxBegin() {
    layer.load();
}

function AjaxSuccessCallback() {
    layer.closeAll();
}


//$(document).ready(function () {
//    $("body").on("click", ".table-rowselected>tbody>tr[data-selectable!='false']", function () {
//        if ($(this).hasClass("selected"))
//            $(this).removeClass("selected");
//        else {
//            $(this).addClass("selected");
//            if (!$(this).parent().parent().hasClass("table-rowselected-multiple"))
//                $(this).siblings().removeClass("selected");
//        }
//    });
//    $("body").on("click", ".table>tbody>tr td.collapsed i", function (e) {
//        e.preventDefault();
//        if ($(this).hasClass("fa-plus-square-o"))
//            $(this).removeClass("fa-plus-square-o").addClass("fa-minus-square-o").parent().parent().next(".tb-child").removeClass("hidden");
//        else
//            $(this).removeClass("fa-minus-square-o").addClass("fa-plus-square-o").parent().parent().next(".tb-child").addClass("hidden");
//    });
//});